package com.example.yjh.widgettest_314yjh;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv;
    private EditText et;
    private Button btngettext,btnsetimage;
    private ImageView iv;
    private ImageButton ibtn;
    private RadioGroup rg;
    private RadioButton rbnan,rbnv;
    private CheckBox cblan,cbzu,cbpai;
    private Button btngetgender,btngetfun;
    private String strfun="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    private void init(){
        tv=(TextView)findViewById(R.id.textView);
        et=(EditText)findViewById(R.id.editText);
        btngettext=(Button) findViewById(R.id.button1);
        btnsetimage=(Button) findViewById(R.id.button2);
        iv=(ImageView)findViewById(R.id.imageView);
        ibtn=(ImageButton) findViewById(R.id.imageButton);
        btngettext.setOnClickListener(this);
        btnsetimage.setOnClickListener(this);
        ibtn.setOnClickListener(this);
        rg=(RadioGroup)findViewById(R.id.radiogroup);
        rbnan=(RadioButton)findViewById(R.id.radioButton1);
        rbnv=(RadioButton)findViewById(R.id.radioButton2);
        cblan=(CheckBox)findViewById(R.id.checkBox);
        cbzu=(CheckBox)findViewById(R.id.checkBox2);
        cbpai=(CheckBox)findViewById(R.id.checkBox3);
        btngetgender=(Button)findViewById(R.id.button3);
        btngetfun=(Button)findViewById(R.id.button4);
        btngetgender.setOnClickListener(this);
        btngetfun.setOnClickListener(this);

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.radioButton1:
                        tv.setText("男");
                        break;
                    case R.id.radioButton2:
                        tv.setText("女");
                        break;
                }
            }
        });
        cblan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    strfun+="篮球+";
                }
                else {
                    strfun=strfun.replace("篮球+","");
                }
                tv.setText(strfun);
            }
        });
        cbpai.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    strfun+="排球+";
                }
                else {
                    strfun=strfun.replace("排球+","");
                }
                tv.setText(strfun);
            }
        });
        cbzu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    strfun+="足球+";
                }
                else {
                    strfun=strfun.replace("足球+","");
                }
                tv.setText(strfun);
            }
        });
    }
    public void onClick(View view){
        switch (view.getId()){
            case R.id.button1:
                String str=et.getText().toString();
                tv.setText(str);
                break;
            case R.id.button2:
                iv.setImageResource(R.drawable.a2);
                break;
            case R.id.imageButton:
                Intent intent=new Intent(MainActivity.this,NewActivity.class);
                intent.putExtra("param1",et.getText().toString());
                int num=3;
                intent.putExtra("param2",num);
                startActivity(intent);
                break;
            case R.id.button3:
                if(rbnan.isChecked()) tv.setText("男");
                if(rbnv.isChecked()) tv.setText("女");
                break;
            case R.id.button4:
                String msg="";
                if (cblan.isChecked())  msg+="篮球+";
                if (cbzu.isChecked())  msg+="足球+";
                if (cbpai.isChecked())  msg+="排球";
                tv.setText(msg);
                break;
        }

    }
}
